package com.open.cloud.rbac.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class HttpUtilNew {
    private static Logger log = LoggerFactory.getLogger(HttpUtilNew.class);

    /**
     * get请求方法
     *
     * @param url
     * @return
     */
    public static String invokeGet(String url, String token, Map<String, String> param) {
        // 返回结果
        Map<String, String> rtnMap = new HashMap<String, String>();
        String result = "";
        String code = "-1";
        HttpClient client = null;
        HttpGet httpGet = null;

        try {
            // 创建httpclient对象
            client = new SSLClient();

            // 创建get方式请求对象
            httpGet = new HttpGet(url);
            httpGet.addHeader("X-Auth-Token", token);
            httpGet.addHeader("Content-Type", "application/json;charset=utf8" );
            HttpResponse resp = client.execute(httpGet);

            if (null != resp) {
                int statusCode = resp.getStatusLine().getStatusCode();
                HttpEntity he = resp.getEntity();
                result = EntityUtils.toString(he, "UTF-8" );
                log.info(result);
                if (200 <= statusCode && statusCode < 300) {
                    code = "0";
                }
                // 鉴权失败
                if (401 == statusCode) {
                    code = "996";
                }
                param.put("code", code);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = e.toString();
        }

        return result;
    }

    /**
     * post请求方法
     *
     * @param url
     * @return
     */
    public static String invokePost(String url, String token, String data, Map<String, String> param) {
        // 返回结果
        Map<String, String> rtnMap = new HashMap<String, String>();
        String result = "";
        String code = "-1";

        HttpClient client = null;
        HttpPost httpPost = null;

        try {
            // 创建httpclient对象
            client = new SSLClient();

            // 创建post方式请求对象
            httpPost = new HttpPost(url);
            httpPost.addHeader("Content-Type", "application/json;charset=utf8" );
            httpPost.addHeader("X-Auth-Token", token);
            // 设置参数到请求对象中
            StringEntity stringEntity = new StringEntity(data, ContentType.APPLICATION_JSON);
            stringEntity.setContentEncoding("utf-8" );
            httpPost.setEntity(stringEntity);
            HttpResponse resp = client.execute(httpPost);

            if (null != resp) {
                int statusCode = resp.getStatusLine().getStatusCode();
                HttpEntity he = resp.getEntity();
                result = EntityUtils.toString(he, "UTF-8" );
                log.info(result);
                if (200 <= statusCode && statusCode < 300) {
                    code = "0";
                }
                // 鉴权失败
                if (401 == statusCode) {
                    code = "996";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = e.toString();
        }

        param.put("code", code);

        return result;
    }

    public static String invokePost(String url, String data, Map<String, String> param) {
        // 返回结果
        Map<String, String> rtnMap = new HashMap<String, String>();
        String result = "";
        String code = "-1";

        HttpClient client = null;
        HttpPost httpPost = null;

        try {
            // 创建httpclient对象
            client = new SSLClient();

            // 创建post方式请求对象
            httpPost = new HttpPost(url);
            httpPost.addHeader("Content-Type", "application/json;charset=utf8" );
            // 设置参数到请求对象中
            StringEntity stringEntity = new StringEntity(data, ContentType.APPLICATION_JSON);
            stringEntity.setContentEncoding("utf-8" );
            httpPost.setEntity(stringEntity);
            HttpResponse resp = client.execute(httpPost);

            if (null != resp) {
                int statusCode = resp.getStatusLine().getStatusCode();
                HttpEntity he = resp.getEntity();
                result = EntityUtils.toString(he, "UTF-8" );
                log.info(result);
                if (200 <= statusCode && statusCode < 300) {
                    code = "0";
                }
                // 鉴权失败
                if (401 == statusCode) {
                    code = "996";
                }
            }
        } catch (Exception e) {
            log.error("调用服务异常", e);
            result = e.toString();
        }

        param.put("code", code);

        return result;
    }

    /**
     * put请求方法
     *
     * @param url
     * @return
     */
    public static String invokePut(String url, String token, String data, Map<String, String> param) {
        // 返回结果
        Map<String, String> rtnMap = new HashMap<String, String>();
        String result = "";
        String code = "-1";

        HttpClient client = null;
        HttpPut httpPut = null;

        try {
            // 创建httpclient对象
            client = new SSLClient();

            // 创建post方式请求对象
            httpPut = new HttpPut(url);
            httpPut.addHeader("Content-Type", "application/json;charset=utf8" );
            httpPut.addHeader("X-Auth-Token", token);

            // 设置参数到请求对象中
            StringEntity stringEntity = new StringEntity(data, ContentType.APPLICATION_JSON);
            stringEntity.setContentEncoding("utf-8" );
            httpPut.setEntity(stringEntity);
            HttpResponse resp = client.execute(httpPut);
            if (null != resp) {
                int statusCode = resp.getStatusLine().getStatusCode();
                HttpEntity he = resp.getEntity();
                result = EntityUtils.toString(he, "UTF-8" );
                log.info(result);
                if (200 <= statusCode && statusCode < 300) {
                    code = "0";
                }
                // 鉴权失败
                if (401 == statusCode) {
                    code = "996";
                }
                param.put("code", code);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = e.toString();

        }

        return result;
    }

    public static String invokePutNoToken(String url, String data, Map<String, String> param) {
        // 返回结果
        Map<String, String> rtnMap = new HashMap<String, String>();
        String result = "";
        String code = "-1";

        HttpClient client = null;
        HttpPut httpPut = null;

        try {
            // 创建httpclient对象
            client = new SSLClient();
            // 创建post方式请求对象
            httpPut = new HttpPut(url);
            httpPut.addHeader("Content-Type", "application/json;charset=utf8" );
            // 设置参数到请求对象中
            StringEntity stringEntity = new StringEntity(data, ContentType.APPLICATION_JSON);
            stringEntity.setContentEncoding("utf-8" );
            httpPut.setEntity(stringEntity);
            HttpResponse resp = client.execute(httpPut);
            if (null != resp) {
                int statusCode = resp.getStatusLine().getStatusCode();
                HttpEntity he = resp.getEntity();
                result = EntityUtils.toString(he, "UTF-8" );
                log.info(result);
                if (200 <= statusCode && statusCode < 300) {
                    code = "0";
                }
                // 鉴权失败
                if (401 == statusCode) {
                    code = "996";
                }
                param.put("code", code);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = e.toString();

        }

        return result;
    }

    /**
     * delete请求方法
     *
     * @param url
     * @return
     */
    public static String invokeDel(String url, String token, String data, Map<String, String> param) {
        // 执行结果
        Map<String, String> rtnMap = new HashMap<String, String>();
        // 返回结果
        String result = "";
        // 默认成功
        String code = "-1";
        HttpClient client = null;
        HttpDelete httpdel = null;
        try {
            // 创建httpclient对象
            client = new SSLClient();
            // 创建post方式请求对象
            httpdel = new HttpDelete(url);
            httpdel.addHeader("Content-Type", "application/json;charset=utf8" );
            httpdel.addHeader("X-Auth-Token", token);
            // 设置参数到请求对象中
            StringEntity stringEntity = new StringEntity(data, ContentType.APPLICATION_JSON);
            stringEntity.setContentEncoding("utf-8" );
            HttpResponse resp = client.execute(httpdel);
            if (null != resp) {
                int statusCode = resp.getStatusLine().getStatusCode();
                HttpEntity he = resp.getEntity();
                if (null == he) {
                    result = "操作成功";
                } else {
                    result = EntityUtils.toString(he, "UTF-8" );
                }
                log.info(result);
                if (200 <= statusCode && statusCode < 300) {
                    code = "0";
                }
                // 鉴权失败
                if (401 == statusCode) {
                    code = "996";
                }
                param.put("code", code);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = e.toString();

        }
        return result;
    }
}
